package com.znufe.exam.chat;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author ys
 * @Mail ys_zuel@163.com
 * @Date 2020/12/14 14:40
 * @Version 1.0
 */
//@RequestMapping(value = "/api")
@RestController
public class ChatController {
    /**
     * 登陆界面
     */
    @CrossOrigin
    @GetMapping("/index")
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    /**
     * 聊天界面
     */
    @CrossOrigin
    @GetMapping("/chat")
//    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public ModelAndView indexChat(String username, String password, HttpServletRequest request) throws UnknownHostException {
        if (StringUtils.isEmpty(username)) {
            username = "匿名用户";
        }
        ModelAndView mav = new ModelAndView("chat");
        mav.addObject("username", username);
        mav.addObject("webSocketUrl", "ws://"+ InetAddress.getLocalHost().getHostAddress()+":"+request.getServerPort()+request.getContextPath()+"/chat");
        return mav;
    }


}
